package com.example.demo.confing;

import com.example.demo.util.JwtTokenUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Cmsinterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader("TOKEN");//从请求头里获取数据
       try{
           //boolean result =JwtTokenUtils.validateToken(token);//校验token
           if (JwtTokenUtils.validateToken(token)){//校验token，
               return true;
           }//校验失败给客户端反映结果
           else {
               response.setContentType("text/html;charset=utf-8");//告诉客户端响应的数据类型
               response.getWriter().println("token失效");//拦截器给客户端反映内容提示
               return  false;
           }
       }catch (Exception e){
           response.setContentType("text/html;charset=utf-8");//告诉客户端响应的数据类型
           response.getWriter().println("token失效");//拦截器给客户端反映内容提示
           return false;
       }
    }
}
